package com.zb_study.Array;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Comparator;

public class ArrayExercise {
    public static void main(String[] args) {

        Book[] books = new Book[4];
        books[0] = new Book("西游记", 50);
        books[1] = new Book("三国演义", 300);
        books[2] = new Book("水浒传", 80);
        books[3] = new Book("红楼梦", 200);

        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
//                Book book1 = (Book)o1;
//                Book book2 = (Book)o2;
//                double priceVal = o2.getPrice() - o1.getPrice();
                double priceVal = o2.getName().length() - o1.getName().length();
                if(priceVal > 0) {
                    return -1;
                } else if (priceVal <0) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });

        System.out.println(Arrays.toString(books));
    }
}

class Book {
    private String name;
    private double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}